package com.wesley.spring.support.resource;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.IOException;

/**
 * ResourceLoaderTest
 *
 * @author WangPanYong
 * @since 2024/01/12
 */
public class ResourceLoaderTest {

    public static void main(String[] args) throws IOException {
//        fileSystemResourceLoader();
        classPathResource();
    }

    /**
     * 读取classpath路径下文件
     *
     * @author WangPanYong
     * @since 2024/01/12 14:49
     **/
    static void classPathResource() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("spring-config.xml");
        System.out.println(classPathResource.getFilename());
        System.out.println(classPathResource.getDescription());
        System.out.println(classPathResource.getURI());
        System.out.println(classPathResource.getURL());
        classPathResource.getInputStream();
        File file = classPathResource.getFile();
        System.out.println(file.getAbsolutePath());
    }

    static void fileSystemResourceLoader() throws IOException {
        FileSystemResourceLoader resourceLoader = new FileSystemResourceLoader();
        Resource resource = resourceLoader.getResource(ResourceUtils.CLASSPATH_URL_PREFIX + "spring-config.xml");
        System.out.println(resource.getFilename());
        System.out.println(resource.getURI());
        System.out.println(resource.getURL());
    }

}
